package com.zhiwang.planet.ui.my

import android.os.Bundle
import android.view.View
import com.zhiwang.planet.R
import com.zhiwang.planet.mvvm.model.RestApi
import com.zhiwang.planet.mvvm.model.request.SmsReq
import com.zhiwang.planet.ui.BaseActivity
import com.zhiwang.planet.util.Toaster
import com.zhiwang.planet.util.arrange
import com.zhiwang.planet.util.arrangeComplete
import com.zhiwang.planet.util.suicideAfterTransition
import com.zhiwang.planet.widget.ColdDownTextView
import io.reactivex.functions.Action
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.activity_change_bind_phone.*
import javax.inject.Inject

class ChangeBindedPhoneActivity : BaseActivity() {


    @Inject
    lateinit var restApi: RestApi


    override fun initInjection() {
        super.initInjection()
        userComponent(this).inject(this)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_change_bind_phone)
        setSupportActionBar(toolbar)
        loadOldPhone()
    }

    private fun loadOldPhone() {
        restApi.getUserInfoEx()
                .arrange(Consumer {

//                    old_tel.setText(it.phone)

                }, {

                })
    }

    fun getSmsCode(view: View) {
        val phone = tel.text.toString()
        restApi.getSms(SmsReq(phone).apply { setTypeChangeBindPhone() })
                .arrangeComplete(Action {

                    (view as ColdDownTextView).start()
                    Toaster.message("验证码已发送")

                }, {
                    Toaster.message(it.message!!)
                })

    }

    fun confirmBind(view: View) {

        val phone = tel.text.toString()

        val sms = smsCodeEditor.text.toString()

        restApi.changeBindPhone(phone, sms)
                .arrangeComplete({
                    Toaster.message("换绑成功")
                    suicideAfterTransition()
                }, {

                })

    }
}